package com.cwvs.helper;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Component;

import com.pingplusplus.Pingpp;
import com.pingplusplus.exception.APIConnectionException;
import com.pingplusplus.exception.APIException;
import com.pingplusplus.exception.AuthenticationException;
import com.pingplusplus.exception.InvalidRequestException;
import com.pingplusplus.exception.PingppException;
import com.pingplusplus.model.Charge;
import com.pingplusplus.model.Refund;

/**
 * ping++工具类
 */
@Component
public class PingPlusPlus {
	/**
	 * 是否为测试服务
	 */
	private boolean isTest = true;
	/**
	 * 测试apiKey
	 */
	private String testApiKey;
	/**
	 * 测试appId
	 */
	private String testAppId;
	/**
	 * 正式apiKey
	 */
	private String serverApiKey;
	/**
	 * 正式appId
	 */
	private String serverAppId;

	public boolean getIsTest() {
		return isTest;
	}

	public void setIsTest(boolean isTest) {
		this.isTest = isTest;
	}

	public String getTestApiKey() {
		return testApiKey;
	}

	public void setTestApiKey(String testApiKey) {
		this.testApiKey = testApiKey;
	}

	public String getTestAppId() {
		return testAppId;
	}

	public void setTestAppId(String testAppId) {
		this.testAppId = testAppId;
	}

	public String getServerApiKey() {
		return serverApiKey;
	}

	public void setServerApiKey(String serverApiKey) {
		this.serverApiKey = serverApiKey;
	}

	public String getServerAppId() {
		return serverAppId;
	}

	public void setServerAppId(String serverAppId) {
		this.serverAppId = serverAppId;
	}

	/**
	 * 支付凭据生成
	 * 
	 * @return
	 */
	public Charge getCharge(Map<String, Object> order) {
		Pingpp.apiKey=getIsTest() ?getTestApiKey():getServerApiKey();
		int amount=0;
		Float bg= MapUtils.getFloat(order, "PRICE");
		bg=bg*100;
		amount=bg.intValue();
		Charge charge = null;
		Map<String, Object> chargeMap = new HashMap<String, Object>();
		// 某些渠道需要添加extra参数，具体参数详见接口文档
		chargeMap.put("amount", getIsTest()?1:amount);
		chargeMap.put("currency", "cny");
		chargeMap.put("subject", MapUtils.getString(order, "ORDER_NO"));
		chargeMap.put("body", MapUtils.getString(order, "ORDER_NO"));
		chargeMap.put("order_no", MapUtils.getString(order, "ORDER_NO"));
		chargeMap.put("channel", MapUtils.getString(order, "PAY_TYPE"));
		chargeMap.put("client_ip", MapUtils.getString(order, "CLIENT_IP"));
		Map<String, String> app = new HashMap<String, String>();
		app.put("id", getIsTest() ? getTestAppId() : getServerAppId());
		chargeMap.put("app", app);
		try {
			// 发起交易请求
			charge = Charge.create(chargeMap);
		} catch (PingppException e) {
			e.printStackTrace();
		}
		return charge;
	}
	
	/**
	 * 通过订单的流水记录查询
	 * @throws APIException 
	 * @throws APIConnectionException 
	 * @throws InvalidRequestException 
	 * @throws AuthenticationException 
	 */
	public Refund retrieveCharge(String chargeId,Map<String,Object> data) throws AuthenticationException, InvalidRequestException, APIConnectionException, APIException{
		Pingpp.apiKey =getIsTest() ?getTestApiKey():getServerApiKey();
		Charge charge=Charge.retrieve(chargeId);
		int amount=0;
		Float bg= MapUtils.getFloat(data, "PRICE");
		bg=bg*100;
		amount=bg.intValue();
		Map<String,Object> refundMap=new HashMap<String,Object>();
	    refundMap.put("amount", amount);
	    refundMap.put("description", "系统自动退款");
	    Refund re = charge.getRefunds().create(refundMap);
		return re;
	}
	
	/**
	 * 通过订单的流水记录查询
	 * @throws APIException 
	 * @throws APIConnectionException 
	 * @throws InvalidRequestException 
	 * @throws AuthenticationException 
	 */
	public Charge retrieveCharge(String chargeId) throws AuthenticationException, InvalidRequestException, APIConnectionException, APIException{
		Pingpp.apiKey =getIsTest() ?getTestApiKey():getServerApiKey();
		Charge charge=Charge.retrieve(chargeId);
		return charge;
	}
}
